package fr.afcepf.atod20.frameWork.controler;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

import fr.afcepf.atod20.frameWork.formBeans.ActionForm;
import fr.afcepf.atod20.frameWork.formBeans.MyBeanPopulate;
import fr.afcepf.atod20.frameWork.pattern.Factory;

/**
 * Servlet implementation class ActionServlet
 */
@WebServlet("/ActionServlet")
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IAction action = null;
		ActionForm actionForm = null;
		Map<String, String> mapDonnees = new HashMap<>();
		mapDonnees = initMap(request);
		Factory factory = null;
		try {
			factory = Factory.getInstance();
			action = factory.getAction(request.getServletPath());
			actionForm = factory.getActionForm(request.getParameter("form"));
			MyBeanPopulate bean = new MyBeanPopulate();
			bean.populateBean(actionForm, mapDonnees);
			action.execute(request, response);
			
		} catch (SAXException|InstantiationException  |IllegalAccessException  | DOMException  |ClassNotFoundException e) {
			
			e.printStackTrace();
		} 
		
		String pageResponse = null;
		pageResponse = factory.getNomPage();
		RequestDispatcher reqDispatch = request.getRequestDispatcher(pageResponse);
		reqDispatch.forward(request, response);
	}

	/**
	 * Méthode de remplissage de la Map avec les parametres récupéré dans la
	 * requete
	 * 
	 * @param request
	 * @return
	 */
	private Map<String, String> initMap(HttpServletRequest request) {
		// On instancie une Map nom/ valeur.
		Map<String, String> mapName = new HashMap<>();
		// utilisation de Enumeration pour itérer sur les parametre récupéré.
		Enumeration<String> enumName = request.getParameterNames();
		// Tant que notre énumération a un élément suivant.
		while (enumName.hasMoreElements()) {
			// Parametre nom a mettre dans la Map.
			String name = enumName.nextElement();
			// On récupère les valeur dans la requete correspondant au parametre
			// name.
			String value = request.getParameter(name);
			// On remplie la Map.
			mapName.put(name, value);
		}
		return mapName;
	}
}
